
//
//  CustomDefineHeader.h
//  LearningSystemMEE
//
//  Created by heyong on 16/3/28.
//  Copyright © 2016年 bjttsx. All rights reserved.
//
/**
 *  UIImage+LL.m
 */
#import "UIImage+LL.h"

@implementation UIImage (LL)

+ (instancetype)waterMarkWithImageName:(NSString *)backgroundImage andMarkImageName:(NSString *)markName{
    
    UIImage *bgImage = [UIImage imageNamed:backgroundImage];
    
    UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);
    [bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];
    
    UIImage *waterImage = [UIImage imageNamed:markName];
    CGFloat scale = 0.3;
    CGFloat margin = 5;
    CGFloat waterW = waterImage.size.width * scale;
    CGFloat waterH = waterImage.size.height * scale;
    CGFloat waterX = bgImage.size.width - waterW - margin;
    CGFloat waterY = bgImage.size.height - waterH - margin;
    [waterImage drawInRect:CGRectMake(waterX, waterY, waterW, waterH)];
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

+ (UIImage *)imageWithimage:(UIImage *)image content:(NSString *)content frame:(CGRect)frame {
    
    CGFloat w=image.size.width;
    CGFloat h=image.size.height;

    UIGraphicsBeginImageContext(image.size);
    // 绘制原生图片
    [image drawAtPoint:CGPointZero];
    // 在原生图上绘制文字
    NSString *str = content;
    // 创建文字属性字典
    NSDictionary *dictionary = @{NSForegroundColorAttributeName:[[UIColor whiteColor]colorWithAlphaComponent:0.8], NSFontAttributeName: [UIFont systemFontOfSize:w/25]};
    // 绘制文字属性
    [str drawInRect:CGRectMake(w/2, h-h/20,  w/2, h/20) withAttributes:dictionary];
    
    // 从当前上下文获取修改后的图片
    UIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext();
    // 结束图形上下文
    UIGraphicsEndImageContext();
    
    
    return imageNew;
}

+ (UIImage*)text:(NSString*)text addToImage:(UIImage*)image{
    //绘制图片上下文
    CGFloat w=image.size.width;
    CGFloat h=image.size.height;
    UIGraphicsBeginImageContext(image.size);

    [image drawInRect:CGRectMake(0,0, w,h)];
    
    //字体绘制到图片的位置和字体属性
    NSDictionary *dictionary = @{NSForegroundColorAttributeName:[[UIColor whiteColor]colorWithAlphaComponent:0.8], NSFontAttributeName:[UIFont fontWithName:@"Arial-BoldMT" size:w/25]};
    NSLog(@"%f--%f--%f--%f--",w/20,w/4,w-w/4,h/10,h/5);
    
    CGFloat height = [text boundingRectWithSize:CGSizeMake(w-w/10, MAXFLOAT)options:NSStringDrawingUsesLineFragmentOrigin  attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial-BoldMT" size:w/25]} context:nil].size.height;
    NSLog(@"%f",height);
    [text drawInRect:CGRectMake(w/20, h-height-h/20, w- w/10,height) withAttributes:dictionary];
    
    UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return newImage;
    
}



@end
